package com.openxt.nbzj.push;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import com.alibaba.fastjson.JSON;
import com.baidu.android.pushservice.PushManager;
import com.baidu.android.pushservice.PushMessageReceiver;
import com.openxt.nbzj.event.Event;
import com.openxt.nbzj.event.EventManager;
import com.openxt.nbzj.util.SystemUtil;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import qs.openxt.libs.util.StringUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016-07-26.
 */
public class PushReceiver extends PushMessageReceiver {
    private Handler mHandler = new Handler();

    @Override
    public void onBind(Context context, int errorCode, String s, String s1, String channelId, String s3) {
        if(errorCode == 0){
            Utils.setLocalStorage(context, Push.PUSH_CHANNELID, channelId);
            Push.addPushTag(context, "notice");
            Push.startPush(context);
        }
    }

    @Override
    public void onUnbind(Context context, int i, String s) {

    }

    @Override
    public void onSetTags(Context context, int i, List<String> list, List<String> list1, String s) {

    }

    @Override
    public void onDelTags(Context context, int i, List<String> list, List<String> list1, String s) {

    }

    @Override
    public void onListTags(Context context, int i, List<String> list, String s) {
        try {
            if (list == null) {
                String tags = Utils.getLocalStorage(context, Push.PUSH_PREFS_NAME);
                JSONArray $tags = JSON.parseArray(tags);
                List<String> tagList = new ArrayList<>();
                for(int m = 0, n = $tags.size(); m < n; m++){
                    tagList.add($tags.getString(m));
                }
                PushManager.setTags(context, tagList);
            } else {
                String tags = Utils.getLocalStorage(context, Push.PUSH_PREFS_NAME);
                JSONArray $tags = JSON.parseArray(tags);
                List<String> tagList = new ArrayList<>();
                for(int m = 0, n = $tags.size(); m < n; m++){
                    tagList.add($tags.getString(m));
                }

                PushManager.setTags(context, tagList);

                List<String> delTagList = new ArrayList<>();
                for (int m = 0, n = list.size(); m < n; m++) {
                    String tag = list.get(m);
                    if (!tagList.contains(tag))
                        delTagList.add(tag);
                }
                if (!delTagList.isEmpty()) {
                    PushManager.delTags(context, delTagList);
                }
            }
        }catch (Exception e){

        }
    }

    @Override
    public void onMessage(Context context, String s, String s1) {

    }

    @Override
    public void onNotificationClicked(final Context context, String title, String description, String custom_content) {
        if (StringUtil.isNull(custom_content))
            return;
        try {
            final JSONObject notify = JSON.parseObject(custom_content);
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        //判断应用是否在后台
                        boolean b = SystemUtil.isBackground(context);
                        if (b) { //打开应用
                            Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(Push.APP_NAME);
                            launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(launchIntent);

                            //延时触发事件
                            mHandler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    try{
                                        Event event = new Event(Push.PUSH_NOTIFIY_KEY);
                                        event.setArg(notify);
                                        EventManager.instance().fireEvent(event);
                                    }catch (Exception e){}
                                }
                            }, 500);
                        } else { //触发事件
                            Event event = new Event(Push.PUSH_NOTIFIY_KEY);
                            event.setArg(notify);
                            EventManager.instance().fireEvent(event);
                        }
                    }catch (Exception e){}
                }
            }, 500);
        } catch (Exception e) {

        }
    }

    @Override
    public void onNotificationArrived(Context context, String s, String s1, String s2) {

    }
}
